package com.cn.ZhiKeDianShang.ui.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.cn.ZhiKeDianShang.AppConfig;
import com.cn.ZhiKeDianShang.R;
import com.cn.ZhiKeDianShang.base.BaseFragment;
import com.cn.ZhiKeDianShang.model.response.IndexBean;
import com.cn.ZhiKeDianShang.presenter.MineFragmentPresenter;
import com.cn.ZhiKeDianShang.presenter.view.MineFragmentView;
import com.cn.ZhiKeDianShang.ui.activity.HistorySearchActivity;
import com.cn.ZhiKeDianShang.ui.activity.MyWebView;
import com.cn.ZhiKeDianShang.ui.activity.NoticlistActivity;
import com.cn.ZhiKeDianShang.ui.activity.OrderListActivity;
import com.cn.ZhiKeDianShang.ui.activity.RelatelistActivity;
import com.cn.ZhiKeDianShang.ui.activity.zyjActivity;
import com.cn.ZhiKeDianShang.util.PaoMaDeng.InnerAdapter;
import com.cn.ZhiKeDianShang.util.PaoMaDeng.MyBannerManger;
import com.cn.ZhiKeDianShang.util.ProgressDialogFragment;
import com.cn.ZhiKeDianShang.util.TakeTurn;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.HttpUtils;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.result.ObjectResult;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.result.Result;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;

/**
 * 我的
 * Created by chengaofu on 2017/6/21.
 */

public class HomeFragment extends BaseFragment<MineFragmentView, MineFragmentPresenter> implements MineFragmentView {


    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.tv_lianmeng_order)
    TextView tvLianmengOrder;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.tv_zyj)
    TextView tvZyj;
    @BindView(R.id.ll_zyj)
    LinearLayout llZyj;
    @BindView(R.id.tv_history)
    TextView tvHistory;
    @BindView(R.id.ll_history)
    LinearLayout llHistory;
    @BindView(R.id.tv_associated)
    TextView tvAssociated;
    @BindView(R.id.ll_associated)
    LinearLayout llAssociated;
    @BindView(R.id.rl_image_text)
    RelativeLayout rlImageText;
    Unbinder unbinder;
    @BindView(R.id.banner)
    ConvenientBanner mBanner;
    @BindView(R.id.mv_recycle)
    RecyclerView mvRecycle;

    private ProgressDialogFragment mProgressDialog;

    InnerAdapter marqueeAdapter;
    MyBannerManger myBannerManger;

    @Override
    protected void initViewsAndEvents() {
        getIndexInfo();
    }


    private void supportRequestWindowFeature(int featureActionBarOverlay) {
    }

    /**
     * @param
     */
    private void getIndexInfo() {
//        DialogHelper
        Map<String, String> params = new HashMap<>();
        HttpUtils.post().url(AppConfig.INDEX_INDEX)
                .params(params)
                .addSecret()
                .build()
                .execute(new BaseCallback<IndexBean>(IndexBean.class) {
                    @Override
                    public void onResponse(ObjectResult<IndexBean> result) {
//                        DialogHelper.dismissProgressDialog();
                        Log.d("Debug", "到达请求这里---");
                        if (Result.checkSuccess(getActivity(), result)) {
                            IndexBean data = result.getData();
                            initRecyclerView(data.getAnnouncement());
                            if (mBanner != null) {
                                mBanner.setMargin();
                                new TakeTurn(getActivity(), data.getBanner(), mBanner, new OnItemClickListener() {
                                    @Override
                                    public void onItemClick(int position) {
                                        //bananer 图现在没有点击事件
//                                        if (!TextUtils.isEmpty(data.getBanner().get(position).getUrl())) {
//                                            Intent intent = new Intent(getActivity(), MyWebView.class);
//                                            intent.putExtra("url", data.getBanner().get(position).getUrl());
//                                            startActivity(intent);
//                                        }
                                    }
                                });
                            }
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
//                        DialogHelper.dismissProgressDialog();
//                        ToastUtil.showErrorNet(LoginActivity.this);
                    }
                });
    }


    private void initRecyclerView(List<IndexBean.AnnouncementBean> list) {
        Log.d("Debug", "到达这里initRecyclerView-------------");
//        if (myBannerManger==null){
        Log.d("Debug", "到达这里myBannerManger里面-------------");
        marqueeAdapter = new InnerAdapter(getActivity(), list, new InnerAdapter.onItemClick() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getActivity(), MyWebView.class);
                intent.putExtra("url", list.get(position).getContent());
                intent.putExtra("title", list.get(position).getTitle());
                intent.putExtra("isFromNotic", true);
                startActivity(intent);
            }
        });
            myBannerManger = new MyBannerManger(getActivity(), mvRecycle);
            mvRecycle.setLayoutManager(myBannerManger);
            mvRecycle.setAdapter(marqueeAdapter);

    }


    @Override
    public void onResume() {
        super.onResume();
        //开始自动翻页
        if (mBanner != null) {
            mBanner.startTurning(3000);
        }
    }


    @Override
    public void onPause() {
        super.onPause();
        //停止翻页
        if (mBanner != null) {
            mBanner.stopTurning();
        }
    }

    @Override
    protected int getErrorLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected int getNormalLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected MineFragmentPresenter createPresenter() {
        return new MineFragmentPresenter(getContext(), this);
    }


    @Override
    public void requestFailed(String message) {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        if (myBannerManger != null) {
            Log.d("Debug", "到达onDestroyView这里");
            myBannerManger.removeHanler();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

    }


    @OnClick({R.id.tv_order, R.id.tv_lianmeng_order, R.id.tv_more, R.id.ll_zyj, R.id.ll_history, R.id.ll_associated})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_order:
                startActivity(new Intent(getActivity(), OrderListActivity.class));
                break;
            case R.id.tv_lianmeng_order:
//                mvRecycle.setLayoutManager(myBannerManger);
//                mvRecycle.setAdapter(marqueeAdapter);
                break;
            case R.id.tv_more:
                startActivity(new Intent(getActivity(), NoticlistActivity.class));
                break;
            case R.id.ll_zyj:
                startActivity(new Intent(getActivity(), zyjActivity.class));
                break;
            case R.id.ll_history:
                startActivity(new Intent(getActivity(), HistorySearchActivity.class));
                break;
            case R.id.ll_associated:
                startActivity(new Intent(getActivity(), RelatelistActivity.class));
                break;
        }
    }
}